package top.lshaci.learning.atguigu.interview3.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * CalcAspect
 *
 * @author lshaci
 * @since 1.0.0
 */
@Aspect
@Component
public class CalcAspect {

    @Before("execution(public int top.lshaci.learning.atguigu.interview3.spring.aop.CalcServiceImpl.*(..))")
    public void before() {
        System.err.println("********** @Before **********");
    }

    @After("execution(public int top.lshaci.learning.atguigu.interview3.spring.aop.CalcServiceImpl.*(..))")
    public void after() {
        System.err.println("********** @After **********");
    }

    @AfterReturning("execution(public int top.lshaci.learning.atguigu.interview3.spring.aop.CalcServiceImpl.*(..))")
    public void afterReturning() {
        System.err.println("********** @AfterReturning **********");
    }

    @AfterThrowing(value = "execution(public int top.lshaci.learning.atguigu.interview3.spring.aop.CalcServiceImpl.*(..))", throwing = "e")
    public void afterThrowing(Throwable e) {
        System.err.println("********** @AfterThrowing **********: " +  e.getMessage());
        e.printStackTrace();
    }

    @Around("execution(public int top.lshaci.learning.atguigu.interview3.spring.aop.CalcServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object retVal = null;
//        try {
            System.err.println("********** @Around::before proceed **********");
            retVal = joinPoint.proceed();
            System.err.println("********** @Around::after proceed **********");
//        } catch (Throwable t) {
//            System.err.println("********** @Around::exception proceed **********");
//        } finally {
//            System.err.println("********** @Around::finally proceed **********");
//        }
        return retVal;
    }

}
